﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace Fayshine.VA.Helpers
{
    public class GridBorderHelper
    {
        ////A0B3C6   #FFA0B3C6
        public static SolidColorBrush _BorderBrush = new SolidColorBrush(ConvertFromString("#FF000000"));
        public static double _BorderThickness = 1;

        /// <summary>
        /// 生成边框
        /// </summary>
        public static void ShowBorder(Grid gd)
        {
            if (gd == null )
            {
                return;
            }

            clearBorder(gd);

            SolidColorBrush BorderColorBrush = _BorderBrush;
            double BorderThickness = _BorderThickness;

            CreateBorder(gd, BorderColorBrush, BorderThickness);

        }

        /// <summary>
        /// 生成边框
        /// </summary>
        public static void ShowBorder(Grid gd, SolidColorBrush BorderColorBrush, double BorderThickness)
        {
            if (gd == null)
            {
                return;
            }

            clearBorder(gd);

            CreateBorder(gd, BorderColorBrush, BorderThickness);

        }

        private static void clearBorder(Grid gd)
        {
            for (int i = gd.Children.Count - 1; i >= 0; i--)
            {
                if (gd.Children[i].ToString() == "System.Windows.Controls.Border")
                {
                    Border re = gd.Children[i] as Border;
                    if (re.Tag == "autoBorder")
                    {
                        gd.Children.RemoveAt(i);
                    }
                }
            }

        }

        private static void CreateBorder(Grid gd, SolidColorBrush BorderBrush, double BorderThickness)
        {
            if (gd.RowDefinitions.Count <= 0)
            {
                for (int j = 0; j < gd.ColumnDefinitions.Count; j++)
                {
                    Border boIn = new Border();

                    boIn.BorderBrush = BorderBrush;
                    boIn.BorderThickness = new Thickness(0, 0, BorderThickness, BorderThickness);
                    boIn.SetValue(Grid.ColumnProperty, j);
                    boIn.SetValue(Grid.RowProperty, 0);
                    boIn.Tag = "autoBorder";
                    gd.Children.Add(boIn);
                }
            }
            else if (gd.ColumnDefinitions.Count <= 0)
            {
                for (int i = 0; i <= gd.RowDefinitions.Count; i++)
                {

                    Border boIn = new Border();

                    boIn.BorderBrush = BorderBrush;
                    boIn.BorderThickness = new Thickness(0, 0, BorderThickness, BorderThickness);
                    boIn.SetValue(Grid.ColumnProperty, 0);
                    boIn.SetValue(Grid.RowProperty, i);
                    boIn.Tag = "autoBorder";
                    gd.Children.Add(boIn);

                }
            }
            else
            {
                for (int i = 0; i <= gd.RowDefinitions.Count; i++)
                {
                    for (int j = 0; j < gd.ColumnDefinitions.Count; j++)
                    {
                        Border boIn = new Border();

                        boIn.BorderBrush = BorderBrush;
                        boIn.BorderThickness = new Thickness(0, 0, BorderThickness, BorderThickness);
                        boIn.SetValue(Grid.ColumnProperty, j);
                        boIn.SetValue(Grid.RowProperty, i);
                        boIn.Tag = "autoBorder";
                        gd.Children.Add(boIn);
                    }
                }
            }

            //画最外面的边框
            Border bo = new Border();
            bo.BorderBrush = BorderBrush;
            bo.BorderThickness = new Thickness(BorderThickness, BorderThickness,0,0);
            bo.SetValue(Grid.ColumnProperty, 0);
            bo.SetValue(Grid.RowProperty, 0);

            if (gd.ColumnDefinitions.Count > 0)
            {
                bo.SetValue(Grid.ColumnSpanProperty, gd.ColumnDefinitions.Count);
            }
            if (gd.RowDefinitions.Count > 0)
            {
                bo.SetValue(Grid.RowSpanProperty, gd.RowDefinitions.Count);
            }
            bo.Tag = "autoBorder";
            gd.Children.Add(bo);
        }


        /// <summary>
        /// 把字符串转成颜色 #A0B3C6
        /// </summary>
        /// <param name="colorString"></param>
        /// <returns></returns>
        public static Color ConvertFromString(string colorString)
        {
            Color color;
            try
            {
                Line line = (Line)XamlReader.Load("<Line xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Fill=\"" + colorString + "\" />");
                color = (Color)line.Fill.GetValue(SolidColorBrush.ColorProperty);
            }
            catch
            {
                color = new Color();
            }

            return color;
        }

    }
}
